/**
 * Created by Yu Jin Xian on 2016/10/27.
 */
function ifStatement() {
    if (i > 25) {
        alert('Greater than 25.');
    } else {
        alert('Less than or equal to 25.');
    }

    function suggest() {
        if (i > 25) {
            alert('Greater than 25.');
        } else if (i < 0) {
            alert('Less than 0.');
        } else {
            alert('Between 0 and 25, inclusive.');
        }
    }
}

function doWhileStatement() {
    var i = 0;
    do {
        i += 2;
    } while (i < 10)
}

function whileStatement() {
    var i = 0;
    while (i < 10) {
        i += 2;
    }
}

function forStatement() {
    var count = 10;
    for (var i = 0; i < count; i++) {
        alert(i);
    }

    function equalTo() {
        var count = 10;
        var i = 0;
        while (i < count) {
            alert(i);
            i++;
        }
    }

    function notUsingVar() {
        var count = 10;
        var i;
        for (i = 0; i < count; i++) {
            alert(i);
        }
    }

    function NotBlockOfScope() {
        var count = 10;
        var i;
        for (i = 0; i < count; i++) {
            alert(i);
        }
        alert(i);       // 10
    }

    function optional() {
        for (; ;) {     // 无限循环

        }

        var count = 10;
        var i = 0;
        for (; i < count;) {

        }
    }
}

function forinStatement() {
    for (var item in window) {
        document.write(item);
    }
}

function labelStatement() {
    start:for (var i = 0; i < count; i++) {
        alert(i);
    }
}

function breakContinueStatement() {
    function breakStatement() {
        var num = 0;
        for (var i = 1; i < 10; i++) {
            if (i % 5 == 0) {
                break;
            }
            num++;
        }
        alert(num);         // 4
    }

    function continueStatement() {
        var num = 0;
        for (var i = 1; i < 10; i++) {
            if (i % 5 == 0) {
                continue;
            }
            num++;
        }
        alert(num);         // 4
    }

    function labelAndBread() {
        var num = 0;

        outermost:
            for (var i = 0; i < 10; i++) {
                for (var j = 0; j < 10; j++) {
                    if (i == 5 && j % 5 == 0) {
                        break outermost;
                    }
                    num++;
                }
            }
        alert(num);         // 55
    }

    function labelAndContinue() {
        var num = 0;

        outermost:
            for (var i = 0; i < 10; i++) {
                for (var j = 0; j < 10; j++) {
                    if (i == 5 && j % 5 == 0) {
                        continue outermost;
                    }
                    num++;
                }
            }
        alert(num);         // 95
    }
}

function withStatement() {
    function split() {
        var qs = location.search.substring(1);
        var hostName = location.hostname;
        var url = location.href;
    }

    with (location) {
        var qs = search.substring(1);
        var hostName = hostname;
        var url = href;
    }
}

function switchStatement() {
    var i = 45;
    if (i = 25) {
        alert('25');
    } else if (i == 35) {
        alert('235');
    } else if (i == 45) {
        alert('45');
    } else {
        alert('Other');
    }

    switch (i) {
        case 25:
        /* 合并两种情形 */
        case 35:
            alert('25 or 35');
            break;
        case 45:
            alert('45');
            break;
        default:
            alert('Other');
    }

    function caseAnyValue() {
        switch ('hellow world') {
            case 'hello' + ' world':
                alert('Greeting was found.');
                break;
            case 'goodbye':
                break;
            default:
                alert('Unexpected message a found.')
        }
    }

    function caseExpression() {
        switch (true) {
            case num < 0:
                alert('Less than 0.');
                break;
            case num >= 0 && num <= 10:
                alert('Less than 0.');
                break;
            case num > 10 && num <= 20:
                alert('Less than 0.');
                break;
            default:
                alert('More than 20.')
        }
    }
}